(function() {
	jQuery('#btnAdd').on("click", function() {
		jQuery('#id').val('');
		jQuery('#areaCode').val('');
		jQuery('#areaName').val('');
		jQuery('#superior').val('');
		jQuery('#buildDate').val('');
		jQuery("#memo").val('');
		jQuery('#saleAreaModal').modal('show');
	});
	// 保存按钮
	jQuery('.saveBtn').on('click', function() {
		if (jQuery("#roleEditForm") && !jQuery("#roleEditForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		}
		;
		var isId = jQuery("#id").val();
		var areaCode = jQuery('#areaCode').val();
		var areaName = jQuery('#areaName').val();
		var superior = jQuery('#superior').val();
		var superiorArea=superior+'-'+areaName;
		var buildDate = jQuery("#buildDate").val();
		var memo = jQuery("#memo").val();
		//验证唯一性
		var d={
				"id":isId,
				"superiorArea":superiorArea
		}
		var url=window.rootPath+"/ui/saleArea/namevalidate";
		jQuery.ajax({
			url:  encodeURI(url),
			cache:false,
			data:d,
            type: 'get',
            dataType: 'json',
			success:function(data){
				if(data.code>0){
            		message(superior+"已存在"+areaName)
				}else{
					// 判断是编辑还是新增
					if (isId == "") {
						var api = window.rootPath + "/api/saleAreas";
						var d = {
							"areaCode" : areaCode,
							"areaName" : areaName,
							"superior" : superior,
							"superiorArea" : superiorArea,
							"memo" : memo
						};
						jQuery.ajax({
							type : 'post',
							url : api,
							data : JSON.stringify(d),
							dataType : 'json',
							contentType : 'application/json;charset=utf-8',
							success : function(data) {
								jQuery('#saleAreaModal').modal('hide');
								serverTable.draw(false);
							}
						});
					} else {
						var data = {
							"id" :isId,
							"areaName" : areaName,
							"areaCode" : areaCode,
							"superior" : superior,
							"superiorArea":superiorArea,
							
							"memo" : memo
						};
						var url = window.rootPath + '/api/saleAreas/' + data.id;
						jQuery.ajax({
							url : encodeURI(url),
							type : 'patch',
							data : JSON.stringify(data),
							dataType : 'json',
							contentType : 'application/json;charset=utf-8',
							success : function() {
								jQuery('#saleAreaModal').modal('hide');
								serverTable.draw(false);
							}
						});
					}
				}
			}
		});

	});
	// 编辑按钮
	jQuery('.editBtn').on('click', function() {
		var selects=getSelect("#dataTable",1);
	  	   if(selects.length>1){
	  		   $.springLemonDialog({
	  		         content: '只能操作一条记录',// 确认文本
	  		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  		             $(this).closeDialog(modal);
	  		         }
	  		   }); 
	  	   }
	  	   else if(selects.length==0){
	  		   $.springLemonDialog({
	  		         content: '请选择要操作的记录',// 确认文本
	  		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  		             $(this).closeDialog(modal);
	  		         }
	  		   }); 
	  	   }else if(selects.length==1){
	  		 var id=selects[0].id;
			jQuery('#id').val(id);
			jQuery('#areaCode').val(selects[0].areaCode);
			jQuery('#areaName').val(selects[0].areaName);
			jQuery('#superior').val(selects[0].superior);
			jQuery('#memo').val(selects[0].memo);
			jQuery('#saleAreaModal').modal('show');  
	  	   }
	});
	// 删除按钮
	jQuery('.deleteBtn').on('click', function() {
		var selects=getSelect("#dataTable",1);
    	var url = window.rootPath + "/ui/saleArea/deleteArea";
 	   delSelect(selects,url,serverTable) 
	});
	
	
	
	var serverData = {};
	serverData.key = "";
	var serverTable;
	jQuery(document).ready(function(){
		serverTable = jQuery('#dataTable').DataTable(
		{
		    "serverSide" : true,
			"ordering" : false,
			"ajax" : {
						"dataType" : "json",
						"type" : "post",
						"cache" : false,
						"url" : window.rootPath+ '/ui/saleArea/getpage',
						"data" : function(d) {return $.extend({}, d,serverData);}
					},
											columns : [
													{
														data : null,
														defaultContent : ""
													},
													{
														data: null,
										                defaultContent: "",
										                render: function (data, type, full, meta) {
										                	var operate ='<input type="checkbox" class="checkchild">';
										                    return operate;

										                }
													},
													{
														data : 'areaName'
													},
													{
														data : 'areaCode'
													},
													{
														data : 'superior'
													},
													{
														data : 'createTime',
														render : function(data,
																type, full,
																meta) {
															if (data) {
																return moment(data).format("YYYY-MM-DD");
															}
														}
													},
													{
														data : 'memo'
													}],
											"drawCallback" : function(oSettings) {
												var k = 0;
												var total = oSettings.aiDisplay.length
														+ oSettings._iDisplayStart;

												for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
													$(
															'td:eq(0)',
															oSettings.aoData[oSettings.aiDisplay[k]].nTr)
															.text(i + 1);
													k++;
												}

												serverTable.off('click', 'tr');
												serverTable
														.on(
																'click',
																'tr',
																function(event) {
																	event = event
																			|| window.event;
																	var target = event.target
																			|| event.scrElement;
																	var data = serverTable
																			.row(
																					this)
																			.data();

																	if (target.className
																			.indexOf('fa-remove') != -1
																			|| target.className
																					.indexOf('delBtn') != -1) {

																		deleteSaleArea(data);
																	}

																	if (target.className
																			.indexOf('fa-pencil') != -1
																			|| target.className
																					.indexOf('editBtn') != -1) {

																		editSaleArea(data);
																	}

																	return;
																});
											}
										});
						var deleteSaleArea = function(data) {
							if (!data)
								return;
							$.springLemonDialog({
								content : '确认删除片区:' + data.areaName + '?',// 确认文本
								clickButton : function(sender, modal, index) {// 按钮事件处理
									// index:0 确认 1：取消
									// sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
									if (index == 0) {
										var url = window.rootPath
												+ "/api/saleAreas/" + data.id;
										jQuery.ajax({
											url : encodeURI(url),
											cache : false,
											type : "delete",
											dataType : "json",
											success : function(data) {
												serverTable.draw(false);
											}
										});
									}
									$(this).closeDialog(modal);
								}

							});

						};

						
						
						jQuery('.searchBtn').on('click', function() {
							serverData.key = jQuery('#keyword').val();
							serverTable.draw();
						})
						// 撤销查询
						jQuery('.resetBtn').on("click", function() {
							jQuery('#keyword').val('');
							serverData.key = '';
							serverTable.draw();
						});

					});
	   ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});
    
    

    
    

})();